import flask
import json
#from pyecharts.charts import Scatter3D, Page

app = flask.Flask(__name__, template_folder='.')

def get_new_random_pos()->list:
	with open('/dev/my_random_kernel', 'rb') as f:
		rand_buf = f.read(12)
		x, y, z = 0, 0, 0
		for i in range(4):
			x <<= 8
			y <<= 8
			z <<= 8
			x += rand_buf[i]
			y += rand_buf[i+4]
			z += rand_buf[i+8]

		x, y, z = x%100, y%100, z%100
		return (x, y, z)

@app.route('/get_data')
def get_data():
	pos = get_new_random_pos()
	with open('data.txt', 'r') as f:
		json_str = f.read()
		json_obj = json.loads(json_str)
		json_obj.append(pos)
	with open('data.txt', 'w') as f:
		json_str = json.dumps(json_obj)
		f.write(json_str)
	with open('./data.txt') as f:
		return json_str

@app.route('/')
def get_root():
	pos = get_new_random_pos()
	with open('data.txt', 'r') as f:
		json_str = f.read()
		json_obj = json.loads(json_str)
		json_obj.append(pos)
	with open('data.txt', 'w') as f:
		f.write(json.dumps(json_obj))
	template = flask.render_template(
			'render.html',
			data=json_obj
		)
	return template

if __name__ == "__main__":
	app.run(host='10.0.2.15', port=5500)
